<?php

# returns an n3 representation of a single triple array
function n3_triple($triple)
{
	extract($triple);
	$object = ($o_type=='obj')? "<{$o}>" : '"'.$o.'"';
	$object .= (!empty($o_lang))? '@'.$o_lang : '';

	return "<{$s}> <{$p}> {$object} .";
}

# returns the php variable within a string
function erdf_var_name($v)
{
	if(preg_match('/\$[a-zA-Z_0-9]+/', $v, $o) ) return $o[0];
	else return false;
}

function triple_has_resource( $triple, $r)
{
	if(erdf_var_name($r))
	{ 
		foreach($triple as $k => $v)
		{
			if (erdf_var_name($r)==erdf_var_name($v) ) return $k;
		}
	}
	else
	{
		foreach($triple as $k => $v)
		{
			if ( ($r)==($v) ) return $k;
		}
	}
	return false;
}



# splits a uri into namespace + term
function split_uri($term_uri)
{
	preg_match('@(.+?[/#])([^/#]+)$@',$term_uri, $p_match);
	array_shift($p_match);
	return  $p_match;	 
}


# returns the biggest array, useful for sorting
function order_by_count($a,$b)
{
	return max(count($a), count($b));
}


# An extention to array_diff:
# It returns an array of all values not present in all arrays given. If '$strict' is true,
# it returns all values not present or not in the same order in all arrays given. The
# arrays to compare must be placed in another array, which is used as argument '$arrays'.
# Returns false if the '$arrays' is invalid.

function array_rdiff ($arrays, $strict = false) {
    # check if argument is valid.
    if (!is_array ($arrays))
        return false;
    foreach ($arrays as $array)
        if (!is_array ($array))
            return false;

    # set working variables
    $diff    = array ();
    $amount  = count ($arrays);
    $needles = array_shift ($arrays);

	foreach($arrays as $array)
	{
		foreach($needles as $needle)
		{
			$needle_key = array_search($needle,$needles);
			if(!in_array($needle, $array)) unset($needles[$needle_key]);
		}
	}
    return array_unique( array_values( $needles));
}

?>